package com.atguigu.ucenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.orderVo.UcenterMemberOrder;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.vo.RegisterVo;
import com.atguigu.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2020-11-06
 */
@RestController
@RequestMapping("/ucenter/member")
@Api(tags = "用户管理")
//@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation("登录")
    @PostMapping("loginUser")
    public R loginUser(@RequestBody Member member) {
        // 登录返回token 通过JWT方式生成
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }
    @ApiOperation("注册")
    @PostMapping("registerUser")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }
    @ApiOperation("根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        // 调用JWT中的方法获取用户的id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        // 通过用户id获取用户信息
        Member member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    @ApiOperation("根据用户id获取用户线信息")
    @GetMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable("id") String id){
        Member member = memberService.getById(id);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);

        return ucenterMemberOrder;
    }

    @ApiOperation("查询某一天的注册人数")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.ok().data("count",count);
    }

}

